Skip to content

Conversation

stevensJourney
Copy link
Collaborator

Overview

We recently introduced OPFS VFS support for the PowerSync web SDK. Unlike IndexedDB-based VFS, which allows a single shared SQLite connection within a shared web worker across multiple tabs, OPFS requires each tab to have its own dedicated SQLite worker.

To manage concurrency, we use Navigator locks, ensuring exclusive access to SQLite connections. Table change notifications are propagated using a BroadcastChannel.

In multi-tab scenarios, a shared sync worker is responsible for syncing operations. This worker requires access to the SQLite database.

  • IndexedDB VFS: A MessagePort to the shared SQLite worker is passed to the sync worker, allowing direct DB access.
  • OPFS VFS: A MessagePort to the last connected tab’s dedicated SQLite worker is passed to the sync worker. This connection is updated as tabs are opened and closed.

The shared sync worker's DB connection is linked to a specific tab. If that tab is frozen or put to sleep (e.g., in Chrome’s Tab Freezing or Edge’s Sleeping Tabs), the connection becomes inaccessible, blocking SQLite queries in the sync worker.

Luckily browsers don't freeze or sleep tabs which are actively holding a web lock. This PR adds a hold on a web lock as soon as the connection is shared to the sync worker. In local testing this prevents tabs from being put to sleep.

Copy link

changeset-bot bot commented Feb 10, 2025

🦋 Changeset detected

Latest commit: 72d39bd

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 2 packages
Name Type
@powersync/web Minor
@powersync/diagnostics-app Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Base automatically changed from fix-tests to main February 20, 2025 11:35
@Chriztiaan Chriztiaan marked this pull request as ready for review March 5, 2025 11:22
Chriztiaan
Chriztiaan previously approved these changes Mar 7, 2025
@Chriztiaan Chriztiaan requested review from rkistner and simolus3 March 10, 2025 06:51
simolus3
simolus3 previously approved these changes Mar 10, 2025
Copy link
Contributor

@simolus3 simolus3 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM apart from one question (which may just be a misunderstanding on my side).

@Chriztiaan Chriztiaan dismissed stale reviews from simolus3 and themself via 2638b24 March 10, 2025 12:48
simolus3
simolus3 previously approved these changes Mar 10, 2025
@Chriztiaan Chriztiaan merged commit 26025f0 into main Mar 10, 2025
6 checks passed
@Chriztiaan Chriztiaan deleted the locks branch March 10, 2025 14:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants